<?php
/**
 * html.class.php
 */

class html
{
	var $url;
	var $db;

    function __construct()
    {
    	global $db, $tablepre;
		$this->db = &$db;
		$this->tablepre = $tablepre;
    }

	function html()
	{
		$this->__construct();
	}

	function index()
	{
		global $siteurl;
		include SITE_ROOT.'data/cache/cache_category.php';
		ob_start();
		include SITE_ROOT.'admin/home.php';
		include template('home');
		$file = SITE_ROOT.'index.shtml';

		return createhtml($file);
	}
	
	function category($cid,$catetype=0,$path,$page=1,$pagesize=13)
	{
		$where = "WHERE fid='$cid'";
		//取子分类 !!!如果不分开取，排序后，数组的下标发生变化，会数据显示会不正常
		include SITE_ROOT.'data/cache/cache_category.php';
		$catedata = $this->db->fetch_first("SELECT * FROM `{$this->tablepre}category` WHERE cid='$cid'");
		$xwcidarr2 = $this->db->fetch_all("SELECT cid FROM `{$this->tablepre}category` WHERE type=0");
		echo '<pre>';
//		print_r($xwcidarr2);
		$xwcidarr = array();
		$i=0;
		foreach($xwcidarr2 as $key=>$value){
			$$value.$key=$value[cid];
//			print_r($$value.$key);
			$xwcidarr[$i] =$value[cid];
			$i++;
//			$xwcidarr += $a;
		}
		$subcate = $this->db->fetch_all("SELECT cid, cname, displayorder FROM `{$this->tablepre}category` $where ORDER BY displayorder");
		foreach ($subcate as $k => $v)
		{
			$sub3id = $v['cid'];
			$subcate[$k]['sub3cate'] = $this->db->fetch_all("SELECT cid, cname, displayorder FROM `{$this->tablepre}category` WHERE fid='$sub3id' ORDER BY displayorder");
		}
		$offset = $pagesize*($page-1);
		$where = "WHERE status>0 AND cateid=$cid";
		$order = 'DESC';
		$total = $this->db->result_first("SELECT count(*) FROM `{$this->tablepre}content` $where");
		$pages = @ceil($total/$pagesize);
		$page = $page > $pages ? $pages : $page;
		$query = $this->db->query("SELECT `cateid`, `title`, `stitle`, `url`, `showimg`, `pubtime` FROM `{$this->tablepre}content` $where ORDER BY `contentid` $order LIMIT $offset, $pagesize");
		while ($row = $this->db->fetch_array($query)) {
			$row['cname'] = $category[$row[cateid]];
			$row['showimg'] = $row['showimg'] ? SITEURL.'/attachments'.$row['showimg'] : SITEURL.'/images/no_picture.gif';
			$data[] = $row;
		}
		$pageurl = multihtml($total, $pagesize, $page, 'index');
		$pagetitle = $catedata['cname'];
		$url = $page == 1 ? "$path/index.shtml" : "$path/index_$page.shtml";
		ob_start();
		switch($catetype){
			case 0:
				include template('default-list');
				break;
			case 1:
				include template('ablum-list');
				break;
			default:
				include template('default-list');
				break;
		}
		$file = SITE_ROOT.$url;
		createhtml($file);
		
		if($pages > $page)
		{
			$page++;
			$message = "共需更新 <font color='red'>$pages</font> 页<br />下面更新第 <font color='red'>{$page}</font> 页";
			$forward = "?action=html&op=category&dosubmit=1&cid=$cid&type=$type&catetype=$catetype&page=$page";
		}
		else
		{
			$message = "更新完成！";
			$forward = '?action=html&op=category';
		}
		showmessage($message, $forward);
		$file = SITE_ROOT.'/'.$path.'/'.$mod.'.shtml';

		return createhtml($file);
	}
	
	function content($contentid)
	{	
		global $siteurl, $timestamp, $administrator, $shopid;
		$c = load("content.class.php");
		$content = $c->get($contentid);

		if(!$content || $content['islink']) return false; //无此ID或者外链
		$url = $content['url'];
		$catetype = $this->db->result_first("SELECT `type` FROM {$this->tablepre}category WHERE cid={$content['cateid']}");
		//读取分类缓存
		if(is_file(CACHE_PATH.'cache_category.php'))
		{
			@include CACHE_PATH.'cache_category.php';
		}
		else
		{
			updatecache('category');
			@include CACHE_PATH.'cache_category.php';
		}
		$category = $_SCACHE['category'];
		
		//处理内容有分页情况
		if(strpos($content['content'], '[page]') !== false)
		{
			list($url_filename) = explode('.', $url);
			$contents = array_filter(explode('[page]', $content['content']));
			$pagenumber = count($contents);
			for($i=1; $i<=$pagenumber; $i++)
			{
				$pageurls[$i] = multihtml($pagenumber, 1, $i, SITEURL.'/'.$url_filename);
			}
			$pageurl = $filesize = 0;
			foreach($contents as $k => $v)
			{
				$content['content'] = $v;
				$pageurl = $pageurls[$k+1];
				ob_start();
				switch($catetype){
					case 0:
						include template('default-article');
						break;
					case 1:
						include template('ablum-article');
						break;
					default:
						include template('default-article');
						break;
				}
				if($k)
				{
					$urls = $url_filename.'_'.($k+1).'.shtml';
				}
				else
				{
					$urls = $url;
				}
				$file = SITE_ROOT.$urls;
				$filesize += createhtml($file);
			}
			return $filesize;
		}
		else
		{
			//$pageurl = '';

			ob_start();
			switch($catetype){
				case 0:
					include template('default-article');
					break;
				case 1:
					include template('ablum-article');
					break;
				default:
					include template('default-article');
					break;
			}
			$file = SITE_ROOT.$url;

			return createhtml($file);
		}
	}
	
	function zhuanti($contentid)
	{
		global $site, $timestamp, $administrator, $siteurl;
		
		$c = load("content.class.php");
		$content = $c->get($contentid);
		if(!$content || $content['islink']) return false; //无此ID或者外链
		$url = $content['url'];
		//专题附属信息
		$special = $this->db->fetch_array($this->db->query("SELECT headextend, banner, template FROM `{$this->tablepre}special` WHERE contentid='$contentid'"));
		$template = $special['template'] && file_exists(SITE_ROOT.'templates/default/'.$special['template'].'.html') ? $special['template'] : 'zhuanti';
		//往期专题回顾
		$speciallist = $this->db->fetch_all("SELECT `title`, `url` FROM `{$this->tablepre}content` WHERE status>0 AND special=1 ORDER BY `contentid` DESC LIMIT 0, 16");
		ob_start();
		include template($template);
		$file = SITE_ROOT.$url;
		return createhtml($file);
	}
	
	function diaoyong()
	{
		if(is_file(CACHE_PATH.'cache_right.php'))
		{
			@include CACHE_PATH.'cache_right.php';
		}
		else
		{
			updatecache('right');
			@include CACHE_PATH.'cache_right.php';
		}
		$r_daren = $_SCACHE['r_daren'];
		$r_huodong = $_SCACHE['r_huodong'];
		$r_zhuanti = $_SCACHE['r_zhuanti'];
	}
}
?>